﻿@using ComputerLock.Platforms
@using System.Globalization
@using ComputerLock.Resources
@inherits LayoutComponentBase
@inject IWindowMoving WindowMoving;
@inject IDialogService DialogService
@inject AppSettings AppSettings
@inject AppSettingWriter AppSettingWriter
@inject IStringLocalizer<Lang> Lang

<style>
    .mud-appbar .mud-toolbar-gutters {
        padding-left: 10px;
        padding-right: 0px;
    }

    .mud-appbar .mud-button-root .mud-icon-button-label .mud-icon-root {
        width: 0.7em;
        height: 0.7em;
    }
</style>

<MudThemeProvider @ref="@_mudThemeProvider" @bind-IsDarkMode="@_isDarkMode" Theme="_customTheme" />
<MudDialogProvider />
<MudSnackbarProvider />

<MudLayout>
    <MudAppBar Class="px-0" Elevation="25" Dense="true" @onmouseup="MouseUp" @onmousedown="MouseDown">

        <div class="d-flex align-center" @onmouseup:stopPropagation="true" @onmousedown:stopPropagation="true">
            <div class="app-icon"></div>
            <MudText Typo="Typo.subtitle2" Class="ml-1 mt-1 cursor-default">@(Lang["Title"])</MudText>
        </div>

        <MudSpacer />

        <div class="d-flex" @onmouseup:stopPropagation="true" @onmousedown:stopPropagation="true">

            @if (AppSettings.AppThemeInt == 0)
            {
                <MudTooltip Text="浅色主题" Delay="600">
                    <MudIconButton Icon="@Icons.Material.Filled.WbSunny"
                                   Size="Size.Medium"
                                   OnClick="ChangeTheme" />
                </MudTooltip>
            }
            else if (AppSettings.AppThemeInt == 1)
            {
                <MudTooltip Text="深色主题" Delay="600">
                    <MudIconButton Icon="@Icons.Material.Filled.DarkMode"
                                   Size="Size.Medium"
                                   OnClick="ChangeTheme" />
                </MudTooltip>
            }
            else
            {
                <MudTooltip Text="使用系统主题" Delay="600">
                    <MudIconButton Icon="@Icons.Material.Filled.AutoMode"
                                   Size="Size.Medium"
                                   OnClick="ChangeTheme" />
                </MudTooltip>
            }
            <MudTooltip Text="设置" Delay="600">
                <MudIconButton Icon="@Icons.Material.Filled.Settings"
                               Size="Size.Medium"
                               OnClick="OpenSettingsDialog" />
            </MudTooltip>
            <TitleBar />
        </div>
    </MudAppBar>
    <MudMainContent Class="mx-4 my-2">
        @Body
    </MudMainContent>

    <Setting @ref="@_settings" />
</MudLayout>
@code {

    private bool _isDarkMode;
    private MudThemeProvider _mudThemeProvider;
    private MudTheme _customTheme;
    private Setting _settings;

    public MainLayout()
    {

    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        _customTheme = new MudTheme()
            {
                Palette = new PaletteLight()
                {
                    Primary = "#fb8c00",
                    Secondary = Colors.Green.Accent4,
                    AppbarBackground = "#F5F5F5",
                    AppbarText = "#fb8c00",
                    TableStriped = "#F5F5F5",
                    TableHover = "#F1F6FD",
                    DarkLighten = "#CACDD1"

                },
                PaletteDark = new PaletteDark()
                {
                    Primary = "#fb8c00",
                    AppbarBackground = "#1111",
                    AppbarText = "#fb8c00",
                    Background = "#333333",
                    Surface = "#333333",
                    TableStriped = "#292929",
                    TableHover = "#182437",
                    DarkLighten = "#494C50",
                    Black = "#858585",
                    TextPrimary = "#FFFFFF",
                    OverlayDark = "#7575757A"
                }
            };
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await SetTheme();
        }
    }

    private async Task ChangeTheme()
    {
        if (AppSettings.AppThemeInt == 0)
        {
            AppSettings.AppThemeInt = 1;
        }
        else if (AppSettings.AppThemeInt == 1)
        {
            AppSettings.AppThemeInt = 2;
        }
        else
        {
            AppSettings.AppThemeInt = 0;
        }
        AppSettingWriter.Save(AppSettings);
        await SetTheme();
    }

    private async Task SetTheme()
    {
        switch (AppSettings.AppThemeInt)
        {
            case 0:
                _isDarkMode = await _mudThemeProvider.GetSystemPreference();
                break;
            case 1:
                _isDarkMode = false;
                break;
            case 2:
                _isDarkMode = true;
                break;
        }
        await InvokeAsync(StateHasChanged);
    }

    private void MouseDown(MouseEventArgs e)
    {
        WindowMoving.MouseDown();
    }

    private void MouseUp()
    {
        WindowMoving.MouseUp();
    }

    private async Task OpenSettingsDialog()
    {
        await _settings.OpenAsync();
    }
}